package com.cloudz.iptv.fragment.content.v1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudz.iptv.R;
import com.cloudz.iptv.api.v1.VodServiceApi;
import com.cloudz.iptv.fragment.BaseFragment;
import com.cloudz.iptv.fragment.content.v1.adapter.SpannableGridAdapter;
import com.cloudz.iptv.fragment.content.v1.adapter.data.ItemImageTextBean;
import com.cloudz.iptv.utils.DisplayUtils;
import com.cloudz.iptv.utils.InPutJson;
import com.cloudz.iptv.utils.ResponseUtils;
import com.cloudz.iptv.utils.RetrofitUtils;
import com.cloudz.iptv.utils.SimplePreference;
import com.owen.tvrecyclerview.widget.TvRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Page3 extends BaseFragment
{
    @BindView(R.id.vodList)
    TvRecyclerView tvRecyclerViewPage3;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        //设置布局横纵间隙
        tvRecyclerViewPage3.setSpacingWithMargins(DisplayUtils.dp2px(view.getContext(), 10), DisplayUtils.dp2px(view.getContext(), 10));


        tvRecyclerViewPage3.setSelectedItemAtCentered(true);


        getVodGroup();

        tvRecyclerViewPage3.setOnItemListener(new TvRecyclerView.OnItemListener()
        {
            @Override
            public void onItemPreSelected(TvRecyclerView parent, View itemView, int position)
            {

            }

            @Override
            public void onItemSelected(TvRecyclerView parent, View itemView, int position)
            {
                onMoveFocusBorder(itemView, 1.1f);
            }

            @Override
            public void onItemClick(TvRecyclerView parent, View itemView, int position)
            {

                SpannableGridAdapter adapter = (SpannableGridAdapter) parent.getAdapter();

                ItemImageTextBean itemLiveChannel = (ItemImageTextBean) adapter.getItem(position);
                String url = itemLiveChannel.url;

                if (url != null)
                {
//                    Intent intent = new Intent(getActivity(), PlayerVodActivity.class);
//                    intent.putExtra("url", url);
//                    startActivity(intent);
                }


            }
        });

        // 离开
        tvRecyclerViewPage3.setOnFocusChangeListener(new View.OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View view, boolean b)
            {
                focusBorder.setVisible(true);
            }
        });

    }


    @Override
    public int getLayoutId()
    {
        return R.layout.activity_pager_3;
    }


    public void getVodGroup()
    {
        // 验证绑定情况
        VodServiceApi vodServiceApi = RetrofitUtils.getInstance().createApi(SimplePreference.getServerUrl(getContext()), VodServiceApi.class);

        vodServiceApi.getMovieList().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    InPutJson inOutJson = ResponseUtils.getInOutJson(response);

                    if (inOutJson.result)
                    {
                        JSONArray vodGroup = inOutJson.getJSONArray();

                        List<ItemImageTextBean> itemImageTextBeans = new ArrayList<>();


                        for (int i = 0, size = vodGroup.size(); i < size; i++)
                        {
                            JSONObject vod = vodGroup.getJSONObject(i);

                            itemImageTextBeans.add(new ItemImageTextBean(vod.getString("name"), vod.getString("url"), vod.getString("image")));

                        }

                        SpannableGridAdapter adapter = new SpannableGridAdapter(getContext(), tvRecyclerViewPage3);

                        adapter.setDatas(itemImageTextBeans);

                        tvRecyclerViewPage3.setAdapter(adapter);

                    }
                } catch (Exception e)
                {
                    Log.d("getVodGroup----->", "vod数据失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {

            }
        });
    }
}
